//
//  ConfirmRealNameController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/3.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "ConfirmRealNameController.h"
#import "LMCountDownButton.h"
#import "SelectBankController.h"
@interface ConfirmRealNameController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *IDNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *bankNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *bankNameTextField;
@property (weak, nonatomic) IBOutlet UIButton *selectBankButton;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet LMCountDownButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;

@property (weak, nonatomic) IBOutlet UIButton *commitButton;
@property (nonatomic,strong) NSString *bankID;
@end

@implementation ConfirmRealNameController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"实名认证";
   [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    LMCountDownButton *sendCodeButton = [[LMCountDownButton alloc] init];
    [sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [sendCodeButton setTitleColor:[UIColor colorWithHexString:@"#FF7321"] forState:(UIControlStateNormal)];
    sendCodeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    sendCodeButton.frame = self.codeButton.bounds;
    [sendCodeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.codeButton addSubview:sendCodeButton];
    
    self.bankID = @"";
    
    if ([self.model.isRealName isEqualToString:@"1"]) {
        self.nameTextField.text = self.model.name;
        self.IDNumberTextField.text = self.model.idcard;
        self.bankNumberTextField.text = self.model.cardNumber;
        self.bankNameTextField.text = self.model.cardBank;
        self.phoneTextField.text = self.model.phone;
    }
    if (self.model.phone) {
        self.phoneTextField.text = @"";
    }
}
- (IBAction)selectedBankButtonClick:(UIButton *)sender {
    SelectBankController * vc = [[SelectBankController alloc] init];
    vc.selectedBankClick = ^(BankListModel * _Nonnull model) {
        self.bankNameTextField.text = model.name;
        self.bankID = model.id;
    };
    [self presentViewController:vc animated:YES completion:nil];
}

// 获取验证码
-(void)click:(LMCountDownButton *)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确电话号码");
        return;
    }
    NSDictionary *dic = @{@"phone":self.phoneTextField.text};
    
    [NetWorkTool POST:sendSms param:[dic mutableCopy] success:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [sender startCountDown];
        ShowToastWithText(@"验证码发送成功");
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
- (IBAction)commitButtonClick:(UIButton *)sender {
    
    self.nameTextField.text = [self filterSpaces:self.nameTextField.text];
    self.nameTextField.text = [self disable_emoji:self.nameTextField.text];
    
    self.IDNumberTextField.text = [self filterSpaces:self.IDNumberTextField.text];
    self.IDNumberTextField.text = [self disable_emoji:self.IDNumberTextField.text];
    
    self.bankNumberTextField.text = [self filterSpaces:self.bankNumberTextField.text];
    self.bankNumberTextField.text = [self disable_emoji:self.bankNumberTextField.text];
    
    self.bankNameTextField.text = [self filterSpaces:self.bankNameTextField.text];
    self.bankNameTextField.text = [self disable_emoji:self.bankNameTextField.text];
    
    self.phoneTextField.text = [self filterSpaces:self.phoneTextField.text];
    self.phoneTextField.text = [self disable_emoji:self.phoneTextField.text];
    
    self.codeTextField.text = [self filterSpaces:self.codeTextField.text];
    self.codeTextField.text = [self disable_emoji:self.codeTextField.text];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"userId"] = USER_ID;
    param[@"name"] = self.nameTextField.text;
    param[@"idcard"] = self.IDNumberTextField.text;
    param[@"phone"] = self.phoneTextField.text;
    param[@"cardNumber"] = self.bankNumberTextField.text;
    param[@"cardBank"] = self.bankNameTextField.text;
    param[@"cardBankNum"] = self.bankID;
    param[@"code"] = self.codeTextField.text;
    
    if ([self.nameTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入姓名");
        return;
    }
    if ([self.IDNumberTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入身份证号");
        return;
    }
    if ([self.bankNumberTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入银行卡号");
        return;
    }
    BOOL kaHaoIsTrue = [self checkBankCardNumber:self.bankNumberTextField.text];
    if (kaHaoIsTrue ==NO) {
        ShowToastWithText(@"输入的银行卡号不正确");
        return ;
    }
    if ([self.bankNameTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请选择开户银行");
        return;
    }
    
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }
    
    if ([self.codeTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入验证码");
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:updateUserRealName param:param success:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } other:^(id dic) {
       [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}

///判断是否为电话号码
- (BOOL)isMobileNumberOnly:(NSString *)mobileNum {
    NSString * MOBILE = @"^((10[0-9])|(11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES) {
        return YES;
    } else {
        return NO;
    }
}
// 过滤空格
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
- (BOOL)checkBankCardNumber:(NSString *)cardNumber {
    int oddSum = 0;     // 奇数和
    int evenSum = 0;    // 偶数和
    int allSum = 0;     // 总和
    // 循环加和
    for (NSInteger i = 1; i <= cardNumber.length; i++) {
        NSString *theNumber = [cardNumber substringWithRange:NSMakeRange(cardNumber.length-i, 1)];
        int lastNumber = [theNumber intValue];
        if (i%2 == 0) {
            // 偶数位
            lastNumber *= 2;
            if (lastNumber > 9){
                lastNumber -=9;
            }
            evenSum += lastNumber;
        }else{
            // 奇数位
            oddSum += lastNumber;
        }
    }
    allSum = oddSum + evenSum;
    // 是否合法
    if (allSum%10 == 0){
        return YES;
    } else {
        return NO;
    }
}
@end
